import time
from pathlib import Path


def read_file_with_generator(filename):
    """利用生成器来读取文件的每一行"""

    file_path = Path(__file__).parent / filename

    with open(file_path, "r", encoding="utf-8") as f:

        for line in f:
            yield line


txt_generator = read_file_with_generator("西游记.txt")

# for line in txt_generator:
#     time.sleep(0.5)
#     print(line)


def process_log_generator(filename):
    """读取日志中带error的日志"""
    file_path = Path(__file__).parent / filename

    with open(file_path, "r", encoding="utf-8") as f:
        for line in f:
            if "ERROR" in line.strip():
                yield line


# 处理java.log日志文件，把文件中记录了error的日志所在的行都打印过滤并打印出来


process_java_log_generatore = process_log_generator("java.log")

for error_line in process_java_log_generatore:
    print(error_line)
